/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewLivraison.java
 *
 * Created on 29 juin 2011, 14:02:13
 */
package View;

import Controller.ControllerCommande;
import Model.Commande;
import Model.Trajet;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author aj48450
 */
public class ViewLivraison extends javax.swing.JFrame {

    private String adresseLivreur = "";
    /** Creates new form ViewLivraison */
    public ViewLivraison(String adresse) {
        initComponents();
        
       
        this.adresseLivreur = adresse;
        
        AjouterAdresseLivreur(adresse);
        AjouterCommande();
        
        
       
    }
    
    private void AjouterCommande( )
    {
        if ( ControllerCommande.getInstance().getNombreCommande()> 0 )
        {
            cboCommande.setEnabled(true);
            
            for (int i=0; i<ControllerCommande.getInstance().getNombreCommande(); i++)  
            {
                cboCommande.addItem(ControllerCommande.getInstance().getCommande(i).getClient().getNom().toString() +  ", " + 
                                            ControllerCommande.getInstance().getCommande(i).getAdresse() + " " + 
                        ControllerCommande.getInstance().getCommande(i).getDateLivraison().toLocaleString());
            }
        }
    }
    
    private void AjouterAdresseLivreur ( String adresse )
    {
         txtAdresseLivreur.setText(adresse);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cboCommande = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtAdresseLivreur = new javax.swing.JTextField();
        imgMap = new javax.swing.JLabel();
        btnModifier = new javax.swing.JButton();
        btnLivrer = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        menuItemFermer = new javax.swing.JMenuItem();

        cboCommande.setEnabled(false);
        cboCommande.setName("cboCommande"); // NOI18N
        cboCommande.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboCommandeItemStateChanged(evt);
            }
        });
        cboCommande.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboCommandeActionPerformed(evt);
            }
        });

        jLabel1.setText("Commande :");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Adresse du livreur : ");
        jLabel2.setName("jLabel2"); // NOI18N

        txtAdresseLivreur.setName("txtAdresseLivreur"); // NOI18N

        imgMap.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        imgMap.setName("imgMap"); // NOI18N

        btnModifier.setText("Modifier");
        btnModifier.setName("btnModifier"); // NOI18N
        btnModifier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModifierActionPerformed(evt);
            }
        });

        btnLivrer.setText("Livrer");
        btnLivrer.setName("btnLivrer"); // NOI18N
        btnLivrer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLivrerActionPerformed(evt);
            }
        });

        jLabel3.setText("Légende :                     L-Emplacement du livreur      R-Emplacement du restaurant       C-Client");
        jLabel3.setName("jLabel3"); // NOI18N

        fileMenu.setMnemonic('f');
        fileMenu.setText("Fichier");

        menuItemFermer.setText("Fermer");
        menuItemFermer.setName("menuItemFermer"); // NOI18N
        menuItemFermer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemFermerActionPerformed(evt);
            }
        });
        fileMenu.add(menuItemFermer);

        menuBar.add(fileMenu);

        setJMenuBar(menuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(imgMap, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(cboCommande, 0, 441, Short.MAX_VALUE)
                            .add(txtAdresseLivreur, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(btnLivrer, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(btnModifier)))
                    .add(jLabel3))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel2)
                            .add(txtAdresseLivreur, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 58, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(cboCommande, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel1)))
                    .add(layout.createSequentialGroup()
                        .add(btnModifier)
                        .add(18, 18, 18)
                        .add(btnLivrer)))
                .add(6, 6, 6)
                .add(jLabel3)
                .add(18, 18, 18)
                .add(imgMap, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Lorsque l'on change le combox box item
    private void cboCommandeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboCommandeActionPerformed
        
        
    }//GEN-LAST:event_cboCommandeActionPerformed

    private void cboCommandeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboCommandeItemStateChanged
       
        int index = this.cboCommande.getSelectedIndex();
        
        if ( ControllerCommande.getInstance().getNombreCommande() > 0 )
        {
            Commande c = ControllerCommande.getInstance().getCommande(index);

            GetGoogleMap(c);
        }
        
    }//GEN-LAST:event_cboCommandeItemStateChanged

    private void menuItemFermerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemFermerActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_menuItemFermerActionPerformed

    private void btnModifierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifierActionPerformed
        cboCommandeItemStateChanged(null);
    }//GEN-LAST:event_btnModifierActionPerformed

    private void btnLivrerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLivrerActionPerformed
        try 
        {
            if ( this.cboCommande.getSelectedIndex()> -1 )
            {
                int index = this.cboCommande.getSelectedIndex();
                Commande c = ControllerCommande.getInstance().getCommande(index);
                int decision =  JOptionPane.showConfirmDialog(null,"Êtes vous sur de vouloir livrer la commande : " + c.GetNoConfirmation()); 
                 
                 
                if (decision == 0)
                {
                    ControllerCommande.getInstance().EnleverCommande(index);
                    this.setVisible(false);
                }
                
              
            }
        }
        catch ( Exception e )
        {
            System.out.println(e.getMessage());
        }
        
    }//GEN-LAST:event_btnLivrerActionPerformed

   private void GetGoogleMap ( Commande c )
   {
        Trajet g = new Trajet(this.txtAdresseLivreur.getText(), c.GetAdresseRestaurant(), c.getAdresse());
        
        boolean ok = g.CalculateDirections();
        
        try   
        {
            if ( ok )
            {
               System.out.println(g.GetURLMap());
               ImageIcon image = new ImageIcon(new URL(g.GetURLMap()));
               imgMap.setIcon(image);
            }
        }
        catch ( Exception e )
        {
            
        }
   }
    
    /**
     * @param args the command line arguments
     */
    public static void main(final String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ViewLivraison(args[0]).setVisible(true);
            }
            
            
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLivrer;
    private javax.swing.JButton btnModifier;
    private javax.swing.JComboBox cboCommande;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JLabel imgMap;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem menuItemFermer;
    private javax.swing.JTextField txtAdresseLivreur;
    // End of variables declaration//GEN-END:variables
}
